package com.mediacloud.app.newsmodule.activity.microlive;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.alibaba.fastjson.JSONObject;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.mediacloud.app.newsmodule.microlive.Lives;
import com.mediacloud.app.newsmodule.microlive.MicroLiveDetailData;
import com.mediacloud.app.reslib.util.DataInvokeUtil;

import java.util.List;

import io.reactivex.Observer;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import retrofit2.Response;


/**
 */

public class MicroLiveDetailPresenter extends BasePresenter<IMicroLiveDetailView>
{
    int refreshInterval=50*1000;
    String id;
    public String refreshDate;
    public MicroLiveDetailPresenter(IMicroLiveDetailView iView)
    {
        super(iView);
        refreshTask=new RefreshTask();
    }
    Handler refreshTask;
    class RefreshTask extends Handler
    {
        @Override
        public void handleMessage(Message msg)
        {
            super.handleMessage(msg);
            if(isDispose)
            {
                refreshTask.removeMessages(0);
                return;
            }
            Log.w("APPTAG","自动刷新数据 refreshDate:"+refreshDate);
            getMicroLiveData(id,true,IMicroLiveDetailView.DESC,refreshDate);
        }
    }

    public void startAutoRefresh()
    {
        if(refreshTask!=null)
            refreshTask.sendEmptyMessageDelayed(0,refreshInterval);
    }
    public void cancelAutoRefresh()
    {
        if(refreshTask!=null)
            refreshTask.removeMessages(0);
    }

    public int mOrder;
    public synchronized void getMicroLiveData(String id, final boolean isAutoRefresh, int order, final String refreshDate)
    {
        mOrder = order;
        this.id = id;
        final String or;
        if (order == IMicroLiveDetailView.DESC) {
            or = "desc";
        } else {
            or = "asc";
        }
        DataInvokeUtil.getMicroLiveData(isAutoRefresh,or,id, refreshDate).subscribe(new Observer<Response<org.json.JSONObject>>() {
            @Override
            public void onSubscribe(@NonNull Disposable d) {

            }

            @Override
            public void onNext(@NonNull Response<org.json.JSONObject> jsonObjectResponse) {
                String result = jsonObjectResponse.body().toString();
                Log.w("APPTAG","refreshDate:"+refreshDate+"+ 是否自动刷新："+isAutoRefresh+" 排序方式："+or+"  取微直播数据返回："+result);
                if(isDispose)
                    return;
                try
                {
                    MicroLiveDetailData microLiveDetailData= JSONObject.parseObject(result,MicroLiveDetailData.class);
                    if(microLiveDetailData.getState())
                    {
                        try
                        {
                            if(microLiveDetailData.getData().getRefresh_time()>0)
                            {
                                refreshInterval=microLiveDetailData.getData().getRefresh_time()*1000;
                            }
                        }
                        catch (Exception e)
                        {
                            e.printStackTrace();
                        }
                        try
                        {
                            getView().updateVideoLiveStream(microLiveDetailData.getData().getMeta().getLivesType3());
                        }
                        catch (Exception e)
                        {
                            getView().updateVideoLiveStream(null);
                        }
                        java.util.List<Lives> lives=null,liveTops=null;
                        if(microLiveDetailData.getData()!=null&&microLiveDetailData.getData().getMeta()!=null)
                        {
                            lives=microLiveDetailData.getData().getMeta().getLives();
                            liveTops=microLiveDetailData.getData().getMeta().getLiveTops();
                        }
                        getView().updateLiveItemData(lives,liveTops,isAutoRefresh,microLiveDetailData.getData().getPaging().getCurrentPage()!=microLiveDetailData.getData().getPaging().getLast_page());
                        if(isAutoRefresh)
                        {
                            refreshTask.sendEmptyMessageDelayed(0,refreshInterval);
                        }
                    }
                    else
                    {
                        onError(null);
                    }
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                    Exception ex=e;
                    onError(null);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                if(isDispose)
                    return;
                getView().errorCall();
                if(isAutoRefresh)
                {
                    refreshTask.sendEmptyMessageDelayed(0,refreshInterval);
                }
            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void destory()
    {
        super.destory();
        cancelAutoRefresh();
        refreshTask=null;
    }

}